How to Select Rows From Pandas DataFrame? – Its Linux FOSS 您所在的位置:网站首页 pandas dataframe dict How to Select Rows From Pandas DataFrame? – Its Linux FOSS

How to Select Rows From Pandas DataFrame? – Its Linux FOSS

2023-03-17 13:28| 来源: 网络整理| 查看: 265

The 2-Dimensional data analysis tool named “DataFrame” is used to store different types of data in rows and columns format. There are a number of operations performed on DataFrame using various functions. These operations are adding rows and columns value, removing rows or columns, sorting, etc.

To select specific rows from Pandas DataFrame, the “loc()” and “iloc()” functions are used in Python. This guide will provide an overview to select rows from Pandas DataFrame using the below content:

Using Python loc() Function Select Rows From Pandas DataFrame Select Rows Based on Specific Condition Select Rows Based on Multiple Conditions Select Rows Based on List of Values in Column Using Python iloc() Function Select Rows From Pandas DataFrame Select Multiple Rows From Pandas DataFrame Using Python loc() Function

In Python, the “loc()” function is used to access data values from a given dataset according to the index label. A dataframe can be selected by selecting a row or column. The following examples are used to select rows from Pandas DataFrame: 

Example 1: Select Rows From Pandas DataFrame

The below code is used to select specific rows from Pandas DataFrame:

Code:

import pandas data = {'Name': ['Alex','Joseph','Lily','Anna', 'Alex'],         'Age': [15, 22, 23, 18, 16], 'Height': [5.3, 5.5, 5.7, 5.1, 4.7]} data_frame = pandas.DataFrame(data, columns= ['Name','Age','Height']) output = data_frame.loc[data_frame['Name'] == 'Alex'] print (output) The DataFrame is created using the “pd.DataFrame()” function by accepting the dictionary and columns name as an argument. The “df.loc()” function is used to select the specific rows “Alex” from the DataFrame.

Output:

The rows containing “Alex” have been displayed.

Example 2: Select Rows Based on Specific Condition

The below code is used to select rows based on specific conditions:

Code:

import pandas data = {'Name': ['Alex','Joseph','Lily','Anna', 'Alex'],         'Age': [15, 22, 23, 18, 16], 'Height': [5.3, 5.5, 5.7, 5.1, 4.7]} data_frame = pandas.DataFrame(data, columns= ['Name','Age','Height']) output = data_frame.loc[data_frame['Age'] >= 20] print (output) The pandas module is imported, and the dictionary named “data” is initialized. The “pd.DataFrame()” function takes the dictionary and columns name as an argument. The “df.loc()” takes the column name “Age” and selects rows according to the specified condition. The “df.loc()” function will select any rows in the “Age” column that contain a number greater than or equal to “20”.

Output:

The rows in the “Age” column having a value greater than “20” have been selected.

Example 3: Select Rows Based on Multiple Conditions

The below code is used to select rows based on multiple defined conditions:

Code-1: (Multiple Condition Using & Operator) import pandas data = {'Name': ['Alex','Joseph','Lily','Anna', 'Alex'],         'Age': [15, 22, 23, 18, 16], 'Height': [5.3, 5.5, 5.7, 5.1, 4.7]} data_frame = pandas.DataFrame(data, columns= ['Name','Age','Height']) output = data_frame.loc[(data_frame['Age'] >= 20) & (data_frame['Height']


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有